package com.chance.cc.crawler.prod.command.meta.xcar;

import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import com.chance.cc.crawler.prod.command.meta.CommonCrawlerKeywordScript;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lt
 * @version 1.0
 * @date 2021-05-18 13:57:45
 *      爱卡经销商关键词
 */
public class XcarDistributorKeyword extends CommonCrawlerKeywordScript {
    private static final String domain = "xcar";

    public static void main(String[] args) {
        distributor();
//        seriesName();

    }

    private static void distributor() {
        String site = "distributor";
        String filePath = "E:\\chance_work\\chance-crawler-development\\crawler-scripts-command-prod\\src\\main\\java\\com\\chance\\cc\\crawler\\prod\\command\\meta\\xcar\\distributor.txt";
        List<String> content = getContent(filePath);
        addKeywords(site,content);
//        delKeywords(site,content);
    }

    private static void seriesName() {
        String site = "series_name";
        String filePath = "E:\\chance_work\\chance-crawler-development\\crawler-scripts-command-prod\\src\\main\\java\\com\\chance\\cc\\crawler\\prod\\command\\meta\\xcar\\series-name.txt";
        List<String> content = getContent(filePath);
        addKeywords(site,content);
//        delKeywords(site,content);
    }

    private static void addKeywords(String site,List<String> keywords){
        for (String keyword : keywords) {
            if(keyword.startsWith("#")){
                continue;
            }
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            keywordCommand.addOrUpdateKeys(crawlerDomainKeys);
        }
    }

    private static void delKeywords(String site,List<String> keywords){
        for (String keyword : keywords) {
            if(keyword.startsWith("#")){
                continue;
            }
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            System.out.println(keyword);
            keywordCommand.delKeys(crawlerDomainKeys);
        }
    }


    public static List<String> getContent(String filepath)  {
        //读取文件
        List<String> lineLists = null;
        try {
            System.out.println(filepath);
            lineLists = Files
                    .lines(Paths.get(filepath), Charset.defaultCharset())
                    .flatMap(line -> Arrays.stream(line.split("\n")))
                    .collect(Collectors.toList());
            lineLists.removeIf(String::isEmpty);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  lineLists;
    }
}
